// pages/myPages/personalCenter/realNamePage/FillInInformation/FillInInformation.js
var request = require('./request.js')

//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showView:'',
    userNameInput:'',
    userICInput:'',
    userN: '',
    passW: '',
    tempFilePaths :'',
    tempFilePathsF:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.getInfo();
    // that.setData({
    //   userN: options.userN
    // })
  },
  getInfo: function (e) {
    var that = this;
    var contacts = [];
    var data = {
      access_token: wx.getStorageSync('createUser').access_token
    };
    request.infoGet(data, function (d) {
      if (d.code == 501){
        wx.showToast({
          title: d.message,
          icon: 'none',
          duration: 1000,
          mask: true
        });
      }else{
        that.setData({
          userN: d.data.real_name,
          passW: d.data.id_number,
          tempFilePaths: d.data.id_card_img,
          tempFilePathsF: d.data.id_card_img2
        })
        wx.showToast({
          title: d.data.status_msg,
          icon: 'none',
          duration: 2000,
          mask: true
        });
      }
        
      that.loginBtnClick();
    });
  },
  UploadFront: function () {
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        var tempFilePaths = res.tempFilePaths
        that.setData({
          tempFilePaths: res.tempFilePaths
        })
        // console.log(res.tempFilePaths)
        wx.setStorage({ key: "tempFilePaths", data: tempFilePaths[0] })
        that.getUpload(tempFilePaths[0], "tempFilePaths")
      }
    })
  },
  ReverseSide: function () {
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        var tempFilePathsF = res.tempFilePaths
        that.setData({
          tempFilePathsF: res.tempFilePaths
        })
        // console.log(res.tempFilePaths)
        wx.setStorage({ key: "tempFilePathsF", data: tempFilePathsF[0] })
        that.getUpload(tempFilePathsF[0], 'tempFilePathsF')
      }
    })
  },
  userNameInput: function (e) {
    this.setData({
      userN: e.detail.value
    })
    this.loginBtnClick();
  },
  userICInput: function (e) {
    this.setData({
      passW: e.detail.value
    })
    this.loginBtnClick();
  },
  loginBtnClick: function () {
    if (this.data.userN.length != 0 && this.data.passW.length != 0) {
      this.setData({
        showView: false
      })
    } else{
      this.setData({
        showView: true
      })
    }
  },
  getUpload: function (filePath, key) {
    var that = this
    wx.uploadFile({
      url: 'https://wallet.kinlink.cn/api/certification/upload-image',
      header: { 'content-type': 'multipart/form-data' },
      method: 'POST',
      filePath: filePath,
      name: 'image',
      formData: {
        'access_token': wx.getStorageSync('createUser').access_token
      },
      success: function (res) {
          var rest = JSON.parse(res.data)
        if (res.statusCode == 200 && rest.code == 200){
          wx.setStorageSync(key, rest.data.urlPath)
        }else{
          wx.showToast({
            title: res.errMsg,
            icon: 'none',
            duration: 500,
            mask: true
          });
        }
      }
    })
  },
  verifyData(){
    var that = this;
    var tempFilePaths = wx.getStorageSync('tempFilePaths')
    var tempFilePathsF = wx.getStorageSync('tempFilePathsF')
    var data = {
      access_token: wx.getStorageSync('createUser').access_token,
      real_name: that.data.userN,
      id_number: that.data.passW,
      id_card_img: tempFilePaths,
      id_card_img2: tempFilePathsF
    };
    request.realSat(data, function (d) {
      console.log(d)
      wx.showToast({
        title: d.message,
        icon: 'none',
        duration: 500,
        mask: true,
        success(){
          if(d.code ==200){
                wx.navigateBack({
                  delta: 1
                })
          }
          //  wx.navigateBack({
          //   delta: 1
          // })
        }
      });
    });
    // wx.navigateBack({
    //   delta: 1
    // })
  },
  getVerifyingCode:function(){
    var userName = this.data.userN
    var t = wx.T.locale == "zh-Hans" ? wx.T.locales['zh-Hans'] : wx.T.locales['en']
    if (/^.+$/.test(userName)){
      var IdCard = this.data.passW
      if (/^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/g.test(IdCard)){
        if (this.data.tempFilePaths != '' && this.data.tempFilePathsF !='' ){
          this.verifyData && this.verifyData()
        }else{
          wx.showToast({
            title: wx.T.locale == "zh-Hans" ? wx.T.locales['zh-Hans'].idTF : wx.T.locales['en'].idTF,
            icon: 'none',
            duration: 1000,
            mask: true
          });
        }
      }else{
        wx.showToast({
          title: IdCard ? t.idCardNo : t.idCardEmpty,
          icon: 'none',
          duration: 1000,
          mask: true
        });
      }
    }else{
      wx.showToast({
        title: userName ? t.nameNo : t.nameEmpty,
        icon: 'none',
        duration: 1000,
        mask: true
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      setLang: wx.T.locale == "zh-Hans" ? wx.T.locales['zh-Hans'] : wx.T.locales['en']

    })
    wx.setNavigationBarTitle({
      title: wx.T.locale == "zh-Hans" ? wx.T.locales['zh-Hans'].RealNameAuthentication : wx.T.locales['en'].RealNameAuthentication
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})